package com.triones.api.controller.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.triones.api.entity.BankCardTypeDO;
import com.triones.api.entity.CustomBankCardStatVO;
import com.triones.api.entity.HelpCategorizeDO;
import com.triones.api.entity.param.*;
import com.triones.api.entity.vo.*;
import com.triones.api.service.BankCardService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 银行卡管理
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/bankCard")
public class BankCardsController extends BaseController {

    private final BankCardService bankCardService;

    /**
     * 用户银行统计列表（分页）
     */
    @SaCheckPermission("system:bankcards:pageList")
    @GetMapping("/pageList")
    public TableDataInfo<CustomMemberBankCardVO> pageList(BankCardParams bankCardParams, PageQuery pageQuery) {
        return bankCardService.pageList(bankCardParams, pageQuery);
    }

    /**
     * 银行列表
     */
    @SaCheckPermission("system:bankcards:list")
    @GetMapping("/list")
    public R<List<BankCardVO>> list(Long uid,String type,String cardStatus) {
        return R.ok(bankCardService.list(uid,type, cardStatus));
    }

    /**
     * 银行卡申请列表（分页）
     */
    @SaCheckPermission("system:bankcardApply:pageList")
    @GetMapping("/apply/pageList")
    public TableDataInfo<BankCardVO> applyPageList(ApplyBankCardParams applyBankCardParams, PageQuery pageQuery) {
        return bankCardService.applyPageList(applyBankCardParams, pageQuery);
    }

    /**
     * 查询卡bin详情
     */
    @GetMapping("/bin")
    public R<CardBinVO> cardBinDetail(@NotBlank(message = "cardBin不能为空") String cardBin) {
        return R.ok(bankCardService.cardBinDetail(cardBin));
    }

    /**
     * 查询银行卡申请详情
     */
    @SaCheckPermission("system:bankcardApply:query")
    @GetMapping("/apply/detail/{id}")
    public R<CustomBankCardVO> applyDetail(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        return R.ok(bankCardService.applyDetail(id));
    }

    /**
     * 银行卡详情
     */
    @SaCheckPermission("system:bankcard:query")
    @GetMapping("/detail/{id}")
    public R<CustomBankCardDetailVO> bankCardDetail(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        return R.ok(bankCardService.bankCardDetail(id));
    }

    /**
     * 银行卡列表（带消费和取现统计）
     */
    @SaCheckPermission("system:bankCardDetail:list")
    @GetMapping("/statList")
    public TableDataInfo<CustomBankCardStatVO> statList(BankCardStatParams params, PageQuery pageQuery) {
        return bankCardService.statList(params, pageQuery);
    }

    /**
     * 消费交易列表
     */
    @SaCheckPermission("system:bankCardAuth:pageList")
    @GetMapping("/auth/txn/pageList")
    public TableDataInfo<CustomBankCardTxnVO> bankCardAuthTxPageList(CustomBankCardTxnParams params, PageQuery pageQuery) {
        return bankCardService.bankCardTxPageList(params, pageQuery);
    }

    /**
     * 取现交易列表
     */
    @SaCheckPermission("system:bankCardAtm:pageList")
    @GetMapping("/atm/txn/pageList")
    public TableDataInfo<CustomBankCardTxnVO> bankCardAtmTxPageList(CustomBankCardTxnParams params, PageQuery pageQuery) {
        return bankCardService.bankCardTxPageList(params, pageQuery);
    }

    /**
     * 卡种（分页）
     */
    @SaCheckPermission("system:bankcardType:pageList")
    @GetMapping("/type/pageList")
    public TableDataInfo<BankCardTypeVO> pageList(BankCardTypeDO dto, PageQuery pageQuery) {
        return bankCardService.typePageList(dto, pageQuery);
    }

    /**
     * 新增卡种
     */
    @SaCheckPermission("system:bankcardType:add")
    @PostMapping("/type")
    public R<Void> addBankcardType(@Validated @RequestBody BankCardTypeDO dto) {
        if (!bankCardService.checkCardSchemeAndCardFormFactor(dto)) {
            return R.fail("新增卡种失败，卡组织为：" + dto.getCardScheme()+ "且卡介质为："  + dto.getCardFormFactor() + "的卡种已存在");
        }
        bankCardService.addBankcardType(dto);
        return R.ok();
    }

    /**
     * 修改卡种
     */
    @SaCheckPermission("system:bankcardType:edit")
    @PutMapping("type")
    public R<Void> editBankcardType(@Validated @RequestBody BankCardTypeDO dto) {
        if (!bankCardService.checkCardSchemeAndCardFormFactor(dto)) {
            return R.fail("修改卡种失败，卡组织为：" + dto.getCardScheme()+ "且卡介质为："  + dto.getCardFormFactor() + "的卡种已存在");
        }
        bankCardService.editBankcardType(dto);
        return R.ok();
    }

    /**
     * 查询卡种详情
     *
     * @param id
     */
    @SaCheckPermission("system:bankcardType:query")
    @GetMapping(value = "/type/{id}")
    public R<BankCardTypeVO> getInfo(@PathVariable Long id) {
        return R.ok(bankCardService.getInfo(id));
    }

    /**
     * 状态修改
     */
    @PutMapping("/type/changeShowStatus")
    public R<Void> changeShowStatus(@RequestBody BankCardTypeDO dto) {
        return toAjax(bankCardService.updateShowStatus(dto));
    }

    /**
     * 删除卡种
     */
    @SaCheckPermission("system:bankcardType:remove")
    @DeleteMapping("/type/{id}")
    public R<Void> removeBankcardType(@PathVariable Long id) {
        bankCardService.removeBankcardType(id);
        return R.ok();
    }
}
